package com.xhwl.centiotdata.handler.vehicle.impl;

import com.xhwl.centiotdata.handler.vehicle.IVehicleIntegrationHandler;
import com.xhwl.centiotdata.service.cent.IVehicleCentService;
import com.xhwl.centiotdata.suppliersystem.vehicle.haikang.HaiKangHandler;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForInFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForOutFeignDTO;
import com.xhwl.common.dto.cent.vehicle.http.haikang.ResponseAccessRecordHaiKangDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForInFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForOutFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.ParkingLotAccessRecordForInBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.ParkingLotAccessRecordForOutBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import com.xhwl.common.pojo.cent.vehicle.converter.feign.AccessRecordFeignServiceConverter;
import com.xhwl.common.pojo.cent.vehicle.converter.http.HttpQueryParameterConverter;
import com.xhwl.common.pojo.cent.vehicle.converter.http.HttpResponseConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HaiKangIntegrationHandler implements IVehicleIntegrationHandler {

    @Autowired
    private HaiKangHandler haiKangHandler;

    @Autowired
    private HttpQueryParameterConverter httpQueryParameterConverter;

    @Autowired
    private HttpResponseConverter httpResponseConverter;

    @Autowired
    private AccessRecordFeignServiceConverter accessRecordFeignServiceConverter;

    @Autowired
    private IVehicleCentService vehicleCentService;

    @Override
    public ParkingLotAccessRecordForInBO queryAccessRecordForIn(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {

        ResponseAccessRecordHaiKangDTO responseAccessRecordHaiKangDTO = haiKangHandler.queryAccessRecord(vehicleIntegrationConfigBO.getVehicleParkingInfoBO(), httpQueryParameterConverter.toQueryAccessRecordHaiKangDTO(queryAccessRecordBO));

        return httpResponseConverter.toParkingLotAccessRecordForInBO(responseAccessRecordHaiKangDTO, vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId());
    }

    @Override
    public ParkingLotAccessRecordForOutBO queryAccessRecordForOut(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {

        ResponseAccessRecordHaiKangDTO responseAccessRecordHaiKangDTO = haiKangHandler.queryAccessRecord(vehicleIntegrationConfigBO.getVehicleParkingInfoBO(), httpQueryParameterConverter.toQueryAccessRecordHaiKangDTO(queryAccessRecordBO));

        return httpResponseConverter.toParkingLotAccessRecordForOutBO(responseAccessRecordHaiKangDTO, vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId());
    }

    @Override
    public void addAccessRecordsForIn(CreateVehicleAccessRecordsForInFeignBO createVehicleAccessRecordsForInFeignBO) {
        log.info("start: Hai Kang add access record for in");
        CreateVehicleAccessRecordsForInFeignDTO createVehicleAccessRecordsForInFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForInFeignDTO(createVehicleAccessRecordsForInFeignBO);

        try {
            vehicleCentService.addAccessRecordsForIn(createVehicleAccessRecordsForInFeignDTO);
            log.info("end: Hai Kang add access record for in");
        } catch (Exception e) {
            log.error("iot调用车行中台的新增入场记录的接口，发生错误，iot的数据无法给到车行中台");
        }
    }

    @Override
    public void addAccessRecordsForOut(CreateVehicleAccessRecordsForOutFeignBO createVehicleAccessRecordsForOutFeignBO) {
        log.info("start: Hai Kang add access record for out");
        CreateVehicleAccessRecordsForOutFeignDTO createVehicleAccessRecordsForOutFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForOutFeignDTO(createVehicleAccessRecordsForOutFeignBO);

        try {
            vehicleCentService.addAccessRecordsForOut(createVehicleAccessRecordsForOutFeignDTO);
            log.info("end: Hai Kang add access record for out");
        } catch (Exception e) {
            log.error("iot调用车行中台的新增出场记录的接口，发生错误，iot的数据无法给到车行中台");
        }
    }

}
